package Bean.Log;

import Action.*;
import Bean.AdministratorEntity;

import Bean.BookInfoEntity;
import Bean.UserinfoEntity;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "SystemLog", schema = "bookloansystem", catalog = "bookloansystem")
public class SystemLogEntity {
    private int id;
    private int UserId;//管理员的id
    private int obId;//操作对象id

    private Timestamp logData;//操作时间
    private String flag;//具体操做
    private String remark;//obid的所属类型


    public static final String remark_book="图书";
    public static final String remark_user="用户";
    public SystemLogEntity(){

    }

    /**
     *
     * @param administratorid 管理员id
     * @param o 当前请求生成日志的类
     * @param state 用于指明图书信息(日志的补充说明)
     */
    private SystemLogEntity(int administratorid, Object o,String  state,int obId,String remark){
        super();
        Session session = HibernateTools.getSession();
        AdministratorEntity administratorEntity;
        if (administratorid==-1){
            //来自系统内部的事件
            administratorEntity=new AdministratorEntity();
            administratorEntity.setName("系统运行中：");
            administratorEntity.setId(-1);
        }
        else {
            administratorEntity = session.get(AdministratorEntity.class, administratorid);
        }

        logData=new Timestamp(System.currentTimeMillis());
        //由于混合功能类UserInfoManage等的出现该方法精确性过差
//        if (o instanceof AddBookInfo) {
//            flag=Setting.SystemLog_flag_AddBook;
//        }else if (o instanceof AddUser){
//            flag=Setting.UserLog_flag_UserAdd;
//        }else if (o instanceof UserInfoManage){
//            flag=Setting.SystemLog_flag_ManagerUserData;
//        }else if (o instanceof BookInfoManage){
//            flag=Setting.SystemLog_flag_ManagerBookData;
//        }
        flag=state;
        this.remark=remark;
        this.obId=obId;
        this.setUserId(administratorEntity.getId());

        session.save(this);
//        HibernateTools.commit(session);//交给调用者

        System.out.println(id+"   系统日志系统:  "+ administratorEntity.getName()+"  " +TimeTools.timeStamp2Date(getLogData(),null)+"  "+flag+"  "+remark+"  对象id:"+obId);

    }
    //向下兼容--remark
    public static boolean addLog(int administratorid,Object o,String state,int id,String remark) {

        try {
            new SystemLogEntity(administratorid,o,state,id,remark);
        } catch (Exception e) {
            System.out.println("系统日志系统故障请检查");
            System.err.println(e.getMessage());
            return false;
        }

        return true;
    }
    //向下兼容--remark(标记)
    public static boolean addLog(int administratorid,Object o,String state,String remark) {

        return addLog(administratorid, o, state, -1, "");
    }
    public static boolean addLog(int administratorid,Object o,String state){

        return addLog(administratorid, o, state, "");
    }



    public String BuildLog(){
        StringBuilder stringBuilder=new StringBuilder();
        Session session=HibernateTools.getSession();
        AdministratorEntity administratorEntity=session.get(AdministratorEntity.class, this.getUserId());
        if (this.getUserId()==-1){
            //来自系统内部的事件
            administratorEntity=new AdministratorEntity();
            administratorEntity.setId(-1);
            administratorEntity.setName("系统");
        }
        stringBuilder.append("ID:  ").append(id).append("     ");
        stringBuilder.append("时间:  ").append(TimeTools.timeStamp2Date(this.getLogData(),null)).append("           ");
        if (administratorEntity==null){
            stringBuilder.append("用户不存在原id:")
                    .append(this.UserId);
        }else stringBuilder.append(administratorEntity.getName()).append("                   ");
        stringBuilder.append("(管理员)").append("     ");
        stringBuilder.append(this.getFlag()).append("     ");
//        stringBuilder.append(this.getFlag()).append("  ");
        if (remark.equals(remark_user)){
            UserinfoEntity userinfoEntity=session.get(UserinfoEntity.class, obId);
            if (userinfoEntity==null){
                stringBuilder.append("已删除用户:").append(obId).append("   ");
            }
            else
                stringBuilder.append(userinfoEntity.getUsername()).append("   ");
        }else if (remark.equals(remark_book)){
            BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, obId);
            if (bookInfoEntity==null){
                stringBuilder.append("图书名称:    ") .append("已删除图书:").append(obId).append("\t");
            }
            else
                stringBuilder.append(bookInfoEntity.getBook_name()).append("(id:").append(obId).append(")").append("   ");
        }

        return stringBuilder.toString();
    }


    @Column(name = "id")
    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Basic
    @Column(name = "userid")
    public int getUserId() {
        return UserId;
    }

    public void setUserId(int userId) {
        UserId = userId;
    }
    @Basic
    @Column(name = "logdata")
    public Timestamp getLogData() {
        return logData;
    }

    public void setLogData(Timestamp logData) {
        this.logData = logData;
    }
    @Basic
    @Column(name = "flag")
    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }
    @Basic
    @Column(name = "remark")
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
    @Basic
    @Column(name = "obId")
    public int getBookId() {
        return obId;
    }

    public void setBookId(int obId) {
        this.obId = obId;
    }
}
